<?php


namespace app\admin\controller\general;


use app\admin\controller\Base;
use app\common\model\Admin;
use think\db\exception\PDOException;
use think\facade\Session;
use think\facade\View;

class Profile extends Base
{
    public function index()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param('row/a', []);
            if ($params['password'] === '') {
                unset($params['password']);
            } else {
                $params['password'] = password_make_or_verify($params['password']);
            }
            $result = false;
            try {
//                $result = Admin::update($params);
                $adminModel = new Admin();
                $adminInfo = $adminModel->find($params['id']);
                $result = $adminInfo->save($params);
            } catch (PDOException $e) {
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                //刷新session
                Session::set('admin', $adminInfo->toArray());
                $this->success('更新成功');
            }
            $this->error('更新失败');
        }
        return View::fetch();
    }

}